package 字符串;

/**
 * @Author Weton Li
 * @Date 2021/3/8 20:35
 */
public class 字符串相加 {

    public static String stringAdd(String num1, String num2) {

        int m = num1.length() - 1;
        int n = num2.length() - 1;
        StringBuffer result = new StringBuffer();
        int carry = 0;
        while (m >= 0 || n >= 0 || carry != 0) {
            int n1 = m >= 0 ? num1.charAt(m) - '0' : 0;
            int n2 = n >= 0 ? num2.charAt(n) - '0' : 0;
            int sum = n1 + n2 + carry;
            result.append(sum % 10);
            carry = sum / 10;
            m--;
            n--;
        }
        return result.reverse().toString();
    }

    public static void main(String[] args) {
        String input1 = "123";
        String input2 = "128";
        System.out.println(stringAdd(input1,input2)); // 251
    }
}
